<?php
namespace app\controllers;


use app\logic\ArticleLogic;
use yii;

class ArticleController extends BackendController
{

    public function actionIndex()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $articleLogic = new ArticleLogic();
        $this->data['breadcrumbs'] = '文章管理';
        return $this->render('index', [
            'article' => $articleLogic->getArticle($query, $page),
            'query' => $query
        ]);
    }

    public function actionQuestion()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $articleLogic = new ArticleLogic();
        $this->data['breadcrumbs'] = '常见问题管理';
        return $this->render('question', [
            'article' => $articleLogic->getQuestion($query, $page),
            'query' => $query
        ]);
    }
    public function actionEdit()
    {
        $articleLogic = new ArticleLogic();
        if(!Yii::$app->request->post()){
            $id = Yii::$app->request->get('article_id');
            $article = $articleLogic->getById($id);
            $this->data['breadcrumbs'] = ['文章管理' => U('article/index'), $article['article_name'] => '', '更新' => ''];
            return $this->render('edit', [
                'article' => $article,
                'theme' => '/assets/',
                'actionUrl' => U('article/edit'),
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('article_id');

            $logicRes = $articleLogic->editArticleById($id, $params);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','更新文章成功');
                jump('article/index');
            } else {
                Yii::$app->getSession()->setFlash('error','更新文章失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }

    }

    public function actionQuestionEdit()
    {
        $articleLogic = new ArticleLogic();
        if(!Yii::$app->request->post()){
            $id = Yii::$app->request->get('article_id');
            $article = $articleLogic->getById($id);
            $this->data['breadcrumbs'] = ['常见问题管理' => U('article/question'), $article['article_name'] => '', '更新' => ''];
            return $this->render('question_edit', [
                'article' => $article,
                'theme' => '/assets/',
                'actionUrl' => U('article/question-edit'),
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('article_id');

            $logicRes = $articleLogic->editQuestionById($id, $params);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','更新文章成功');
                jump('article/question');
            } else {
                Yii::$app->getSession()->setFlash('error','更新文章失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }

    }


    public function actionSaveStatus()
    {
        $id = Yii::$app->request->get('article_id');
        $articleLogic = new ArticleLogic();
        $logicRes = $articleLogic->saveStatus($id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','更改文章状态成功');
        } else {
            Yii::$app->getSession()->setFlash('error','更改文章状态失败');
        }
        return $this->redirect(U('article/question'));
    }

    public function actionAdd()
    {
        if(!Yii::$app->request->post()){
            $this->data['breadcrumbs'] = ['文章管理' => U('article/index'), ' 添加文章' => ''];
            return $this->render('add', [
                'theme' => '/assets/',
                'actionUrl' => U('article/add')
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $articleLogic = new ArticleLogic();
            $logicRes = $articleLogic->addArticle($params);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','新增文章成功');
                jump('article/question');
            } else {
                Yii::$app->getSession()->setFlash('error','新增文章失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }




}